/*
 * $Id: XmlParserFactory.java,v 1.2 2009/07/10 20:04:18 navdeep_mahajan Exp $
 * 
 * Copyright (C) 2008 General Electric Company. All Rights Reserved.
 * 
 * This software is the confidential and proprietary information of the General
 * Electric Company (GE). You shall not disclose this software and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with GE.
 * 
 * GE MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
 * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
 * NON-INFRINGEMENT. GE SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 * AS A RESULT OF USING, MODIFYING, OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES.
 */
package com.ge.healthcare.autosc.processor.xml;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

import org.springframework.util.Assert;

import com.ge.healthcare.autosc.common.ASCLogger;
import com.ge.healthcare.autosc.common.database.dao.IXmlParserInfoDAO;
import com.ge.healthcare.autosc.common.database.pojo.CfgXmlParserInfo;
import com.ge.healthcare.autosc.common.exceptions.XmlParserFactoryException;
import com.ge.healthcare.autosc.common.util.xml.dom.IXmlParser;

public class XmlParserFactory {

	private static IXmlParserInfoDAO xmlParserInfoDAO;

	public void init() {
		Assert
				.notNull(xmlParserInfoDAO,
						"xmlParserInfoDAO is not defined. Use setXmlParserInfoDAO to provide one");
	}

	public static IXmlParser getXmlParser(String parserName)
			throws XmlParserFactoryException {
		String methodName = "getXmlParser";
		IXmlParser parser = null;
		if (parserName != null) {
			ASCLogger.debug(XmlParserFactory.class, methodName,
					"get parser with parser name " + parserName);
			CfgXmlParserInfo xmlParserInfo = xmlParserInfoDAO
					.getXmlParserInfoByName(parserName);
			if (xmlParserInfo != null) {
				String errorStr = "Failed createing Xml Parser for class "
						+ xmlParserInfo.getXmlParserClass();
				try {
					Class<?> theClass = Class.forName(xmlParserInfo
							.getXmlParserClass());
					Class<?> parTypes[] = new Class<?>[1];
					parTypes[0] = String.class;
					Constructor<?> ct = theClass.getConstructor(parTypes);
					Object args[] = new Object[1];
					args[0] = xmlParserInfo.getXmlSchemaFile();
					parser = (IXmlParser) ct.newInstance(args);
				} catch (ClassNotFoundException cnfe) {
					throw new XmlParserFactoryException(errorStr, cnfe);
				} catch (InstantiationException ie) {
					throw new XmlParserFactoryException(errorStr, ie);
				} catch (IllegalAccessException iae) {
					throw new XmlParserFactoryException(errorStr, iae);
				} catch (NoSuchMethodException nsme) {
					throw new XmlParserFactoryException(errorStr, nsme);
				} catch (InvocationTargetException ite) {
					throw new XmlParserFactoryException(errorStr, ite
							.getCause());
				}
			} else {
				ASCLogger
						.error(XmlParserFactory.class, methodName,
								"CfgParserInfo not found for parser name "
										+ parserName);
			}
		}
		return parser;
	}

	public void setXmlParserInfoDAO(IXmlParserInfoDAO xmlParserInfoDAO) {
		XmlParserFactory.xmlParserInfoDAO = xmlParserInfoDAO;
	}
}
